package edu.hunau.cxb18.orm.mapper.provide;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import edu.hunau.cxb18.orm.pojo.Product;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * @author hb
 * @create 2020-03-04 10:40
 */
public class ProductSqlProvide {

    public String queryByLikeDataSQL(Map<String, String> data) {
        return new SQL() {{
            SELECT("*");
            FROM("t_product");
            if (data.size() == 1) {
                if (data.containsKey("name") && !data.get("name").equals("")) {
                    WHERE("name like concat('%',#{name},'%')");
                 }
                if (data.containsKey("pdesc") && !data.get("pdesc").equals("")) {
                    WHERE("pdesc like concat('%',#{pdesc},'%')");
                }
            }
            if(data.size()==2){
               if(data.containsKey("name") && data.containsKey("pdesc")){
                   WHERE("name like concat('%',#{name},'%') and pdesc like concat('%',#{pdesc},'%')");
                }
            }
         }}.toString();
    }

    public String insertProductByList(@Param("pros") List<Product> pros) {

        return new SQL() {
            {
                INSERT_INTO("t_porudct");
                INTO_COLUMNS("name", "pdesc", "pic", "price", "stock");
                for (Product pro : pros) {
                    INTO_VALUES(pro.getName(), pro.getPdesc(), pro.getPic(), pro.getPrice() + "", pro.getStock() + "");
                }

            }
        }.toString();

    }
}
